import React, { useEffect } from 'react';
import { Form, Input, Select, DatePicker, Card, Button, message } from 'antd';
import { useNavigate, useParams } from 'react-router-dom';
import { createPartyMember, updatePartyMember, getPartyMember } from '@/api/partyMember';
import type { PartyMemberDTO } from '@/api/partyMember';
import dayjs from 'dayjs';

const { Option } = Select;

const PartyMemberForm: React.FC = () => {
  const navigate = useNavigate();
  const { id } = useParams<{ id: string }>();
  const [form] = Form.useForm();
  const isEdit = !!id;

  useEffect(() => {
    if (isEdit) {
      fetchData();
    }
  }, [id]);

  const fetchData = async () => {
    try {
      const data = await getPartyMember(Number(id));
      form.setFieldsValue({
        ...data,
        joinDate: data.joinDate ? dayjs(data.joinDate) : undefined,
      });
    } catch (error) {
      // 错误已在request.ts中处理
    }
  };

  const handleSubmit = async (values: any) => {
    try {
      const data: PartyMemberDTO = {
        ...values,
        joinDate: values.joinDate?.format('YYYY-MM-DD'),
      };

      if (isEdit) {
        await updatePartyMember(Number(id), data);
        message.success('更新成功');
      } else {
        await createPartyMember(data);
        message.success('创建成功');
      }

      navigate('/party-members');
    } catch (error) {
      // 错误已在request.ts中处理
    }
  };

  return (
    <Card title={isEdit ? '编辑党员信息' : '新增党员'}>
      <Form
        form={form}
        layout="vertical"
        onFinish={handleSubmit}
      >
        <Form.Item
          name="className"
          label="所在班级"
          rules={[{ required: true, message: '请输入所在班级' }]}
        >
          <Input placeholder="请输入所在班级" />
        </Form.Item>

        <Form.Item
          name="position"
          label="职务"
          rules={[{ required: true, message: '请输入职务' }]}
        >
          <Input placeholder="请输入职务" />
        </Form.Item>

        <Form.Item
          name="name"
          label="姓名"
          rules={[{ required: true, message: '请输入姓名' }]}
        >
          <Input placeholder="请输入姓名" />
        </Form.Item>

        <Form.Item
          name="idCard"
          label="身份证号"
          rules={[
            { required: true, message: '请输入身份证号' },
            { pattern: /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[0-9Xx]$/, message: '请输入正确的身份证号' },
          ]}
        >
          <Input placeholder="请输入身份证号" />
        </Form.Item>

        <Form.Item
          name="identifier"
          label="标识号"
          rules={[{ required: true, message: '请输入标识号' }]}
        >
          <Input placeholder="请输入标识号" />
        </Form.Item>

        <Form.Item
          name="joinDate"
          label="入党时间"
          rules={[{ required: true, message: '请选择入党时间' }]}
        >
          <DatePicker style={{ width: '100%' }} />
        </Form.Item>

        <Form.Item
          name="positionLevel"
          label="职级"
          rules={[
            { required: true, message: '请选择职级' },
            { pattern: /^(校级干部|院级干部|班级干部|组长)$/, message: '职级只能是：校级干部、院级干部、班级干部、组长' },
          ]}
        >
          <Select placeholder="请选择职级">
            <Option value="校级干部">校级干部</Option>
            <Option value="院级干部">院级干部</Option>
            <Option value="班级干部">班级干部</Option>
            <Option value="组长">组长</Option>
          </Select>
        </Form.Item>

        <Form.Item>
          <Button type="primary" htmlType="submit">
            {isEdit ? '保存' : '创建'}
          </Button>
          <Button style={{ marginLeft: 8 }} onClick={() => navigate('/party-members')}>
            取消
          </Button>
        </Form.Item>
      </Form>
    </Card>
  );
};

export default PartyMemberForm; 